﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.IO;

namespace NopiCommon
{
    public class FileUploadCommon
    {
        /// <summary>
        /// 上传文件。
        /// </summary>
        /// <param name="fu_Upload"></param>
        /// <param name="p_MyPage"></param>
        /// <param name="strMessage"></param>
        /// <param name="fileExt"></param>
        /// <param name="fileSize"></param>
        /// <param name="filePath"></param>
        /// <param name="fileType"></param>
        public static void FileUpload(FileUpload fu_Upload, Page p_MyPage,out string strMessage,out string fileExt,out int fileSize,out string filePath,out  string fileType)
        {
            strMessage ="文件上传失败！";
            fileExt = string.Empty;
            fileSize = 0;
            filePath = string.Empty;
            fileType = string.Empty;

            if (!fu_Upload.HasFile)
            {
                strMessage = "请选择需要上载的文件！";
            }
            else
            {
                fileExt = System.IO.Path.GetExtension(fu_Upload.FileName); //当前文件的扩展名。
                fileType = checktype(fileExt); //文档类型 例如：Word、Excel、Txt等
                fileSize = fu_Upload.PostedFile.ContentLength; //上传文件。
 
                string severFilePath = p_MyPage.MapPath(@"~\UpFileTraining\") + fu_Upload.FileName;
                filePath = severFilePath; //上传文件的路径
                

                //判断上传文件文件是否存在 如果存在则删除
                if (!File.Exists(severFilePath))
                {
                    File.Delete(severFilePath);
                }
                fu_Upload.SaveAs(severFilePath);
                
                strMessage = "OK";
            }
        }


        /// <summary>
        /// 通过上传文件的后缀判断文档的类型。
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        private static string checktype(string filename)
        {
            string ContentType;
            switch (filename)
            {
                case ".asf":
                    ContentType = "video/x-ms-asf ";
                    break;
                case ".avi":
                    ContentType = "video/avi ";
                    break;
                case ".doc":
                    ContentType = "application/msword "; break;
                case ".zip":
                    ContentType = "application/zip "; break;
                case ".xls":
                case ".xlsx":
                    ContentType = "application/vnd.ms-excel "; break;
                case ".gif":
                    ContentType = "image/gif "; break;
                case ".jpg":
                    ContentType = "image/jpeg "; break;
                case ".jpeg":
                    ContentType = "image/jpeg "; break;
                case ".wav":
                    ContentType = "audio/wav "; break;
                case ".mp3":
                    ContentType = "audio/mpeg3 "; break;
                case ".mpg":
                    ContentType = "video/mpeg "; break;
                case ".mepg":
                    ContentType = "video/mpeg "; break;
                case ".rtf":
                    ContentType = "application/rtf "; break;
                case ".html":
                    ContentType = "text/html "; break;
                case ".htm":
                    ContentType = "text/html "; break;
                case ".txt":
                    ContentType = "text/plain "; break;
                default:
                    ContentType = "application/octet-stream ";
                    break;
            }
            return ContentType;
        }
    }
}
